package com.shlh.saas.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.shlh.saas.entity.UserEmail;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface UserEmailMapper extends BaseMapper<UserEmail> {

    @Select("SELECT * FROM user_email WHERE user_id = #{userId}")
    List<UserEmail> selectByUserId(@Param("userId") Long userId);

    @Update("update user_email set email_password = #{password}, email_state = #{state}, email_provider = #{emailProvider} where user_id = #{userId} and email = #{email} ")
    int updateByIdAndEmail(@Param("userId") Long userId, @Param("email") String email, @Param("password") String password, @Param("state") Integer state, @Param("emailProvider") String emailProvider);

    @Select("SELECT * FROM user_email WHERE user_id = #{userId} AND email = #{email}")
    UserEmail selectByUserIdAndEmail(@Param("userId")Long userId,@Param("email") String email);

    @Select("SELECT * FROM user_email WHERE  id = #{id}")
    UserEmail selectEmailById(@Param("id")Long id);

    @Update("UPDATE user_email SET email_password = #{deviceCode}, access_token = #{accessToken}, refresh_token = #{refreshToken}, token_expires_at = #{expiry} WHERE user_id = #{userId} AND email = #{email}")
    int updateEmail(@Param("userId")Long userId,@Param("email")String email, @Param("deviceCode") String deviceCode,@Param("accessToken") String accessToken,@Param("refreshToken") String refreshToken,@Param("id") LocalDateTime expiry);

    @Update("UPDATE user_email SET client_id = #{clientId}, client_secret =#{clientSecret}, tenant_id = #{tenantId}, redirect_uri = #{redirectUri}  WHERE user_id = #{userId} AND email = #{email}")
    int updateEmailByOuth(@Param("userId")Long userId,@Param("email") String email,@Param("clientId") String clientId,@Param("clientSecret") String clientSecret,@Param("tenantId") String tenantId,@Param("redirectUri") String redirectUri);

    @Update("UPDATE user_email SET email_state = #{state}  WHERE user_id = #{userId} AND email = #{email}")
    int saveEmailSuccess(@Param("userId") Long userId, @Param("email") String email, @Param("state") int state);

    @Delete("DELETE FROM user_email WHERE user_id = #{userId} AND email = #{email}")
    int deleteEmail(@Param("userId")Long userId,@Param("email") String email);


//    int insert(UserEmail userEmail);

}
